﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;

public partial class controls_admin_AdminUserEdit : Pages_Base
{
    private DataTable objDT;
    private string exceptRole = string.Empty;
    private string plusRole = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            loadSetting();
            loadListBoxGroup(lsbParent, 0);
            loadData();
            lsbParent.SelectedIndex = 0;
            lsbParent_SelectedIndexChanged(null, null);
        }
    }
    protected void loadData()
    {
        try
        {
            if (Request["u_id"] != null)
            {
                base.OpenConnection();
                DataTable objDT = BO_Sec_Administrators.Search(Convert.ToInt32(Convert.ToInt32(Request["u_id"])));
                if (objDT != null && objDT.Rows.Count > 0)
                {
                    txtAddress.Text = objDT.Rows[0]["ADDRESS"].ToString();
                    txtEmail.Text = objDT.Rows[0]["EMAIL"].ToString();
                    txtFullName.Text = objDT.Rows[0]["FULL_NAME"].ToString();
                    txtPhone.Text = objDT.Rows[0]["PHONE_NUMBER"].ToString();
                    txtUsername.Text = objDT.Rows[0]["USER_NAME"].ToString();
                    txtUsername.Enabled = false;
                    string parentlist = objDT.Rows[0]["GROUP_ADMIN_ID"].ToString();
                    string[] parentarr = parentlist.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string per in parentarr)
                    {
                        foreach (ListItem item in lsbParent.Items)
                            if (item.Value.Equals(per))
                                item.Selected = true;
                    }
                    string pluslist = objDT.Rows[0]["PLUS_ROLE"].ToString();
                    string[] plusarr = pluslist.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string per in plusarr)
                    {
                        foreach (ListItem item in lsbPlusRole.Items)
                            if (item.Value.Equals(per))
                                item.Selected = true;
                    }
                    string exceptlist = objDT.Rows[0]["EXCEPT_ROLE"].ToString();
                    string[] exceptarr = exceptlist.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string per in exceptarr)
                    {
                        foreach (ListItem item in lsbExceptRole.Items)
                            if (item.Value.Equals(per))
                                item.Selected = true;
                    }
                }
                else
                    divMessage.InnerHtml = O2S_Message.Warning("Cảnh Báo", "Bản ghi này không tồn tại hoặc đã bị xóa .");
            }
        }
        catch (Exception ex) { divMessage.InnerHtml = O2S_Message.Warning("Lỗi", "Phát hiện lỗi: " + ex.Message); }
        finally { base.CloseConnection(); }
    }
    private void getExceptRole(int admin_id)
    {
        exceptRole = exceptRole + admin_id.ToString() + ";";
        DataTable c_RoleDT = BO_Sec_Admin_Modules.SelectParent(1, admin_id);
        if (c_RoleDT != null && c_RoleDT.Rows.Count > 0)
        {
            foreach (DataRow c_dr in c_RoleDT.Rows)
            {
                getExceptRole(Convert.ToInt32(c_dr["ADMIN_MODULE_ID"]));
            }
        }
    }
    private void getPlusRole(int admin_id)
    {
        plusRole = plusRole + admin_id.ToString() + ";";
        DataTable c_RoleDT = BO_Sec_Admin_Modules.SelectParent(1, admin_id);
        if (c_RoleDT != null && c_RoleDT.Rows.Count > 0)
        {
            foreach (DataRow c_dr in c_RoleDT.Rows)
            {
                getPlusRole(Convert.ToInt32(c_dr["ADMIN_MODULE_ID"]));
            }
        }
    }
    protected void btnEditUser_Click(object sender, EventArgs e)
    {
        try
        {
            if (Request["u_id"] != null)
            {
                string parent = string.Empty;
                foreach (ListItem item in lsbParent.Items)
                {
                    if (item.Selected)
                        parent = parent + item.Value + ";";
                }
                foreach (ListItem item in lsbExceptRole.Items)
                {
                    if (item.Selected)
                        getExceptRole(Convert.ToInt32(item.Value));
                }
                foreach (ListItem item in lsbPlusRole.Items)
                {
                    if (item.Selected)
                        getPlusRole(Convert.ToInt32(item.Value));
                }
                DataTable dtData = BO_Sec_Administrators.Search(Convert.ToInt32(Request["u_id"]));
                if (dtData.Rows.Count > 0)
                {
                    base.OpenConnection();
                    DataTable objDT = (DataTable)Session["admin"];
                    string strResult = BO_Sec_Administrators.Update(Convert.ToInt32(Request["u_id"]), parent, plusRole, exceptRole, txtFullName.Text, txtUsername.Text, dtData.Rows[0]["pass_word"].ToString(), txtPhone.Text, txtAddress.Text, txtEmail.Text, Convert.ToDateTime(dtData.Rows[0]["LAST_PASSWORD_CHANGE"].ToString()), Convert.ToInt32(dtData.Rows[0]["LOGIN_FALURE_COUNT"].ToString()), Convert.ToDateTime(dtData.Rows[0]["LAST_LOGIN_DATE"].ToString()), 1, Convert.ToDateTime(dtData.Rows[0]["CREATED_DATE"].ToString()), dtData.Rows[0]["CREATED_BY"].ToString(), DateTime.Now, /*objDT.Rows[0]["user_name"].ToString()*/"ngocnh", Boolean.Parse(dtData.Rows[0]["status"].ToString()));
                    if (strResult != "0")
                        divMessage.InnerHtml = O2S_Message.Success("Hệ Thống", "Thông tin quản trị viên đã được cập nhật!");
                    else
                        divMessage.InnerHtml = O2S_Message.Error("Hệ Thống", "Có lỗi trong quá trình xử lý!"); ;
                }
            }
        }
        catch { }
        finally { base.CloseConnection(); }
    }
    protected void loadListBoxGroup(ListBox lsb, int parent)
    {
        try
        {
            objDT = BO_Sec_Group_Admin.SelectParent(1, parent);
            if (objDT != null && objDT.Rows.Count > 0)
            {
                foreach (DataRow dr in objDT.Rows)
                {
                    lsb.Items.Add(new ListItem(dr["GROUP_NAME"].ToString(), dr["GROUP_ADMIN_ID"].ToString()));
                    loadListBoxChildGroup(lsb, Convert.ToInt32(dr["GROUP_ADMIN_ID"].ToString()), "");
                }
            }
            else lsb.Items.Add(new ListItem("---Chưa có nhóm nào---", "0"));
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void loadListBoxChildGroup(ListBox lsbChild, int parentid, string identify)
    {
        try
        {
            DataTable objDTChild = BO_Sec_Group_Admin.SelectParent(1, parentid);
            if (objDTChild != null && objDTChild.Rows.Count > 0)
            {
                identify += "--- ";
                foreach (DataRow drChild in objDTChild.Rows)
                {
                    lsbChild.Items.Add(new ListItem(identify + drChild["GROUP_NAME"].ToString(), drChild["GROUP_ADMIN_ID"].ToString()));
                    loadListBoxChildGroup(lsbChild, Convert.ToInt32(drChild["GROUP_ADMIN_ID"].ToString()), identify);
                }
            }
        }
        catch (Exception ex) { throw ex; }
    }
    private void loadSetting()
    {
        objDT = BO_Settings.SelectByGroupId(6, 1);
        foreach (DataRow dr in objDT.Rows)
        {
            string value = dr["SETTING_VALUE"].ToString();
            switch (dr["SETTING_KEY"].ToString())
            {
                case "txtNoteGroup":
                    txtNoteGroup.InnerText = value;
                    break;
                case "txtNoteFullname":
                    txtNoteFullname.InnerText = value;
                    break;
                case "txtNoteUserName":
                    txtNoteUserName.InnerText = value;
                    break;
                case "txtNotePhone":
                    txtNotePhone.InnerText = value;
                    break;
                case "txtNoteAddress":
                    txtNoteAddress.InnerText = value;
                    break;
                case "txtNoteEmail":
                    txtNoteEmail.InnerText = value;
                    break;
                case "txtNotePlusRole":
                    txtNotePlusRole.InnerText = value;
                    break;
                case "txtNoteExceptRole":
                    txtNoteExceptRole.InnerText = value;
                    break;
            }
        }
    }
    private void bindLsbPlusRole()
    {
        lsbPlusRole.Items.Clear();
        string list_group = string.Empty;
        foreach (ListItem item in lsbParent.Items)
        {
            if (item.Selected)
                list_group = list_group + item.Value + ";";
        }
        string list_admin_module = ";" + BO_Sec_Administrators.GetPlusRole(list_group);
        base.bindAdminModules(lsbPlusRole, 0, list_admin_module);
    }
    private void bindLsbExceptRole()
    {
        lsbExceptRole.Items.Clear();
        string list_group = string.Empty;
        foreach (ListItem item in lsbParent.Items)
        {
            if (item.Selected)
                list_group = list_group + item.Value + ";";
        }
        string list_admin_module = ";" + BO_Sec_Administrators.GetPlusRole(list_group);
        base.bindAdminModules(lsbExceptRole, 0, list_admin_module);
    }
    protected void lsbParent_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindLsbExceptRole();
        bindLsbPlusRole();
    }
}
